/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;


import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import pojo.ChuongTrinhKhuyenMai;
import pojo.SanPham;
import util.HibernateUtil;

/**
 *
 * @author ANNM
 */
public class SanPhamKMDAO {
     public static List<SanPham> LaySP(){
        List<SanPham> ds=null;
        Session session=HibernateUtil.getSessionFactory().openSession();
        String tinhtrang="true";
        String hql="select distinct sp.sanPham from SanPham sp where sp.tinhtrang= "+tinhtrang;
        Query query=session.createQuery(hql);
        ds=query.list();
        session.close();
        return ds;
    }
     public static List<ChuongTrinhKhuyenMai> LayKM(){
         List<ChuongTrinhKhuyenMai> ds=null;
         Session session=HibernateUtil.getSessionFactory().openSession();
         String tinhtrang="true";
         String hql="select km from ChuongTrinhKhuyenMai km ";
        Query query=session.createQuery(hql);
        ds=query.list();
        session.close();
         return ds;
     }
     public static ChuongTrinhKhuyenMai LayMa(int masp){
         ChuongTrinhKhuyenMai km=null;
         Session session=HibernateUtil.getSessionFactory().openSession();
         String tinhtrang="true";
         String hql="select km from ChuongTrinhKhuyenMai km where km.sanPham.maSanPham="+masp;
        Query query=session.createQuery(hql);
        km=(ChuongTrinhKhuyenMai)query.uniqueResult();
        session.close();
         return km;
     }
     public static List<ChuongTrinhKhuyenMai> LaySPKM(int mach){
         List<ChuongTrinhKhuyenMai> sp=null;
         Session session=HibernateUtil.getSessionFactory().openSession();
         String tinhtrang="true";
         String hql="select km from ChuongTrinhKhuyenMai km,SanPham sp where km.sanPham.maSanPham=sp.maSanPham and sp.cuaHang.maCuaHang="+mach;
        Query query=session.createQuery(hql);
        sp=query.list();
        session.close();
         return sp;
     }
     public static int ThemSPKM(ChuongTrinhKhuyenMai spkm){
        Session session= HibernateUtil.getSessionFactory().openSession();
        Transaction tran=null;
        int kq=1;
        try{
        tran= session.beginTransaction();
        session.saveOrUpdate(spkm);
        tran.commit();
        }catch(HibernateException ex){
            tran.rollback();
            System.err.println(ex);
            kq=0;
        }finally{
            session.close();
        }
        return kq;
    }
     public static int XoaSPKM(ChuongTrinhKhuyenMai spkm){
         int kq=1;
          Session session= HibernateUtil.getSessionFactory().openSession();
        Transaction tran=null;
        
        try{
        tran= session.beginTransaction();
        session.delete(spkm);
        tran.commit();
        }catch(HibernateException ex){
            tran.rollback();
            System.err.println(ex);
            kq=0;
        }finally{
            session.close();
        }
        return kq;
         
     }
     public static int SuaSPKM(ChuongTrinhKhuyenMai spkm){
         int kq=1;
          Session session= HibernateUtil.getSessionFactory().openSession();
        Transaction tran=null;
        
        try{
        tran= session.beginTransaction();
        session.saveOrUpdate(spkm);
        tran.commit();
        }catch(HibernateException ex){
            tran.rollback();
            System.err.println(ex);
            kq=0;
        }finally{
            session.close();
        }
        return kq;
         
     }
    
}
